#include "solar/trunk/NListWidget.h"

NListWidget::NListWidget(QWidget *parent, bool autoExit):winflag(autoExit){
    setParent(parent);
    delayHideTimer.setInterval(3000);
    delayHideTimer.setSingleShot(true);
    setItemDelegate(delegate);
    unvoidw->setAttribute(Qt::WA_TranslucentBackground);
    setCornerWidget(unvoidw);
    connect(&delayHideTimer, &QTimer::timeout, this, [this](){setVisible(false); emit msev_invisible();});
}


QList<QListWidgetItem *> NListWidget::all(){
    int row = 0;
    QList<QListWidgetItem *> alls;
    while (row < count())
    {
        alls.append(item(row));
        row++;
    }
    return alls;
}

std::pair<int, int> NListWidget::getMaxlenght(){
    int row = 0;
    uint maxlenght = 0;
    while (row < count())
    {
        QListWidgetItem *item = this->item(row);
        QFontMetrics metrics(item->font());
        uint textlenght = metrics.horizontalAdvance(item->text());
        if (textlenght > maxlenght) maxlenght = textlenght;
        row++;
    }
    return std::make_pair(maxlenght, row);
}

int NListWidget::minor(){
    if (count())
    {
        return count() - 1;
    }
    else
    {
        return count();
    }
}


void NListWidget::del(int row){
    QListWidgetItem *current_item = this->item(row);
    if (current_item)
    {
        QWidget *itemWidget = this->itemWidget(current_item);
        if (itemWidget)
        {
            removeItemWidget(current_item);
            delete itemWidget;
        }
        if (current_item != NULL)
        {
            unselected();
            delete this->takeItem(row);
        }
    }
}


void NListWidget::clear(){
    int row = 0;
    while (row < count())
    {
        del(row);
        row++;
    }
}

void NListWidget::sclear(){
    int row = 0;
    while (row < count())
    {
        this->closePersistentEditor(item(row));
        row++;
    }
    QListWidget::clear();
}

void NListWidget::unselected(){
    foreach(QListWidgetItem *item, selectedItems())
    {
        item->setSelected(false);
    }
}


void NListWidget::desc(){
    for(int row = 0; row < count(); row++)
    {
        QListWidgetItem *item = this->item(row);
        QWidget *itemwin = itemWidget(item);
        if (itemwin)
        {
            QString objectname = QString::number(row);
            QObjectList objlist = itemwin->children();
            if (!objlist.isEmpty())
            {
                foreach(QObject* obj, objlist)
                {
                    QString realobj = obj->objectName().replace(QRegularExpression("\\d+"), objectname);
                    obj->setObjectName(realobj);
                }
            }
            itemwin->setObjectName(objectname);
        }
    }
}


void NListWidget::put(int &give){
    give = count() - 1;
}


void NListWidget::enterEvent(QEnterEvent *){
    if (winflag) delayHideTimer.stop();
}


void NListWidget::leaveEvent(QEvent *){
    if (winflag) delayHideTimer.start(); //鼠标焦点离开，隐藏组件
}


void NListWidget::changeEvent(QEvent *event){
    if (event->type() == QEvent::ActivationChange)
    {
        //非活跃状态，隐藏
        if (!isActiveWindow())
        {
            delayHideTimer.stop();
            setVisible(false);
            emit msev_invisible();
        }
    }
}

